/**
 *
 * © Bahadir Zeren 2011
 * bzeren@gmail.com
 * No warranties, express or implied, are made for this program.
 *
 */

package org.baatar.net.test;

import org.baatar.net.client.TCPClient;
import org.baatar.net.message.IRequest;

/**
 * Main class of the client for Math TCP Service.
 * It randomly sends messages for calculation.
 * 
 * @author B_ZEREN
 */
public class MathTCPClient {
    public static void main(String args[]) throws Exception {

        final String hostName = "127.0.0.1";
        final int portNo = 4321;

        Thread caller = new Thread(new Runnable() {

            @Override
            public void run() {
                TCPClient client = new TCPClient();
                client.setHostName(hostName);
                client.setPortNo(portNo);

                while (true) {

                    double r = Math.random();
                    String operation = null;

                    if (r > 0.75)
                        operation = "+";
                    else
                        if (r > 0.5)
                            operation = "-";
                            else
                                if (r > 0.25)
                                    operation = "*";
                                else
                                    operation = "/";

                    double a = Math.random();
                    double b = Math.random();
                    IRequest request = new Request("MathTCPClient", operation, a, b);

                    try {
                        Response response = (Response) client.sendMessage(request, Double.class);

                        if (response.getException() != null)
                            System.out.println(response.getException().getMessage());
                        else
                            System.out.println(a + " " + operation + " " + b + " = " + response.getResult());

                        Thread.sleep(100);
                    } catch (Exception ex) {
                        System.out.println(ex.getMessage());
                    }

                }
            }
        });

        caller.start();
    }
}
